﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Domain.HU;
using HiStaff.iSystem;
using Janus.Windows.GridEX;

namespace HiStaff.Control
{
    internal partial class HiEmployeeFilter : UserControl
    {
        private bool _filterMultiEmployee = true;
        private List<GridEXColumn> extraColumns;
        public List<GridEXColumn> ExtraColumns
        {
            get { return extraColumns; }
            set { extraColumns = value; }
        }
        public bool FilterMultiEmployee
        {
            get { return _filterMultiEmployee; }
            set { _filterMultiEmployee = value; }
        }
        public List<EMPLOYEE_SELECTION> EmployeeSelected { get; set; }
        private List<EMPLOYEE_SELECTION> lstSelection;
        public HiEmployeeFilter()
        {
            InitializeComponent();
        }

        private void HiEmployeeFilter_Load(object sender, EventArgs e)
        {
            try
            {
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    if (this.extraColumns != null)
                    {
                        foreach (var col in this.extraColumns)
                        {
                            grdFilter.RootTable.Columns.Add(col);
                        }
                    }
                    grbButton.Visible = _filterMultiEmployee;
                    grbFilter.Visible = _filterMultiEmployee;
                    if (EmployeeSelected == null) EmployeeSelected = new List<EMPLOYEE_SELECTION>();
                    BindData();
                }
            }
            catch
            {
            }
        }
        private void BindData()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                lstSelection = Service.ProfileClient.GetEmployeeSelection(new EMPLOYEE_SEARCH
                {
                    ORGID = trvOrg.OrgId,
                    TERMINATE = chkTerminate.Value
                });
                if (EmployeeSelected != null && EmployeeSelected.Count > 0)
                {
                    foreach (var item in EmployeeSelected)
                    {
                        EMPLOYEE_SELECTION objCheck = lstSelection.Find(obj => obj.ID == item.ID && obj.CODE == item.CODE);
                        if (objCheck != null)
                            lstSelection.Remove(objCheck);
                    }
                }

                grdEmployee.SetDataBinding(lstSelection, "");
                grdEmployee.Refresh();
                grdFilter.SetDataBinding(EmployeeSelected, "");
                grdFilter.Refresh();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Parent.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Parent.Hide();
        }

        private void trvOrg_AfterSelect(object sender, TreeViewEventArgs e)
        {
            BindData();
        }

        private void chkTerminate_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Parent.Hide();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            EMPLOYEE_SELECTION objCheck;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                foreach (var item in grdEmployee.GetCheckedRows())
                {
                    if (item != null)
                    {
                        objCheck = EmployeeSelected.Find(obj => obj.ID == ((EMPLOYEE_SELECTION)item.DataRow).ID);
                        if (objCheck == null)
                        {
                            EmployeeSelected.Add((EMPLOYEE_SELECTION)item.DataRow);
                        }
                    }
                }

                foreach (var item in EmployeeSelected)
                {
                    lstSelection.Remove(item);
                }

                grdEmployee.Refetch();
                grdFilter.Refetch();

                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Parent.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            EMPLOYEE_SELECTION objCheck;
            try
            {

                this.Cursor = Cursors.WaitCursor;
                foreach (var item in grdFilter.GetCheckedRows())
                {
                    if (item != null)
                    {
                        objCheck = lstSelection.Find(obj => obj.ID == ((EMPLOYEE_SELECTION)item.DataRow).ID);
                        if (objCheck == null)
                        {
                            lstSelection.Add((EMPLOYEE_SELECTION)item.DataRow);
                        }
                    }
                }

                foreach (var item in lstSelection)
                {
                    EmployeeSelected.Remove(item);
                }

                grdEmployee.Refetch();
                grdFilter.Refetch();

                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grdEmployee_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (e.Row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (FilterMultiEmployee)
                    {
                        //var objCheck = EmployeeSelected.Find(obj => obj.ID == ((EMPLOYEE_SELECTION)e.Row.DataRow).ID);
                        //if (objCheck == null)
                        //{
                        //    EmployeeSelected.Add((EMPLOYEE_SELECTION)e.Row.DataRow);
                        //}

                        //foreach (var item in EmployeeSelected)
                        //{
                        //    lstSelection.Remove(item);
                        //}

                        //grdEmployee.Refetch();
                        //grdFilter.Refetch();
                    }
                    else
                    {
                        EmployeeSelected = new List<EMPLOYEE_SELECTION>();
                        EmployeeSelected.Add((EMPLOYEE_SELECTION)e.Row.DataRow);
                        this.Parent.Hide();
                    }
                }
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Parent.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grdFilter_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            try
            {
                //this.Cursor = Cursors.WaitCursor;
                //if (e.Row.RowType == Janus.Windows.GridEX.RowType.Record)
                //{
                //    var objCheck = lstSelection.Find(obj => obj.ID == ((EMPLOYEE_SELECTION)e.Row.DataRow).ID);
                //    if (objCheck == null)
                //    {
                //        lstSelection.Add((EMPLOYEE_SELECTION)e.Row.DataRow);
                //    }
                //    foreach (var item in EmployeeSelected)
                //    {
                //        lstSelection.Remove(item);
                //    }
                //    grdEmployee.Refetch();
                //    grdFilter.Refetch();
                //}
                //this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Parent.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
